#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "processmanager.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    mapWidget = new MapWidget(ui->mainStack);
    ui->mainStack->addWidget(mapWidget);

    mapCtrl = new MapCtrl(this);
    connect(ui->dashBoard, SIGNAL(driveMode(bool)), mapCtrl, SLOT(setDriveMode(bool)));
    connect(mapWidget, SIGNAL(gotoPos(QPointF,int)), mapCtrl, SLOT(gotoPosReq(QPointF,int)));//前往目标点
    connect(mapCtrl, SIGNAL(carPos(QPointF, int)), mapWidget, SLOT(recvCarPos(QPointF,int)));//更新汽车坐标
    connect(mapCtrl, SIGNAL(carState(bool)), ui->controlBoard, SLOT(carStateChanged(bool)));//汽车状态改变
    connect(mapCtrl, SIGNAL(carInfo(QStringList)), ui->dashBoard, SLOT(carInfo(QStringList)));
    connect(mapCtrl, SIGNAL(blockInfo(QStringList)), mapWidget, SLOT(recvBlockPos(QStringList)));
    connect(mapCtrl, SIGNAL(carPorpMap(QVariantMap)), this, SLOT(recvPropMap(QVariantMap)));

    winSettings = new SettingWidget();
    connect(winSettings, SIGNAL(updateMap()), mapWidget, SLOT(updatePath()));//更新地图
    connect(winSettings, SIGNAL(updateStation()), mapWidget, SLOT(updateStation()));//更新站点
    connect(winSettings, SIGNAL(mapLoad(QString)), mapCtrl, SLOT(mapLoad(QString)));
    ui->dashBoard->setProperty("voltage", 12.6);
    ui->dashBoard->setProperty("visionState", V_NORMAL);
    ui->dashBoard->setProperty("lidarState", L_NORMAL);
    ui->dashBoard->setProperty("stateGps", G_RTK);
    ui->dashBoard->setProperty("controlState", C_REMOTE);
    ui->dashBoard->setProperty("state4G", true);
    ui->dashBoard->setProperty("speed", 80);

//    winMapRecord = new MapRecordWidget();
//    ui->page_record->layout()->addWidget(winMapRecord);

    ui->tabWidget->setTabText(0, "导航");
//    ui->tabWidget->setTabText(1, "录制");

    connect(ui->controlBoard, SIGNAL(viewStateChanged(bool)), this, SLOT(mapViewChanged(bool)));
    connect(ui->controlBoard, SIGNAL(sysSettings()), this, SLOT(sysSettings()));
//    connect(ui->controlBoard, SIGNAL(ctrlStart()), this, SLOT(carStart()));
//    connect(ui->controlBoard, SIGNAL(ctrlStop()), this, SLOT(carStop()));
//    connect(mapCtrl, SIGNAL(carInfo(QStringList)), ui->controlBoard, SLOT(carInfo(QStringList)));
}

MainWidget::~MainWidget()
{
    delete ui;
    winSettings->deleteLater();
}

void MainWidget::recvPropMap(QVariantMap m)
{
    QVariantMap::iterator i;

    for(i = m.begin(); i != m.end(); i++)
    {
        QByteArray key = i.key().toLocal8Bit();
        qDebug()<<i.key()<<i.value();
        ui->dashBoard->setProperty(key.data(), i.value());
        ui->controlBoard->setProperty(key.data(), i.value());
    }
}

void MainWidget::carStart()
{
    mapCtrl->carCtrlStart();
}

void MainWidget::carStop()
{
    mapCtrl->carCtrlStop();
}

void MainWidget::sysSettings()
{
    winSettings->showMaximized();
}

void MainWidget::mapViewChanged(bool checked)
{
    mapWidget->setGlobalView(checked);
}
